<template>
	<div v-loading="Loading">
		<el-select :disabled="belongProject == 'elderlyMeal'" v-model="project" placeholder="请选择项目" style="width: 280px" @change="handleChange">
			<el-option v-for="item in dataList" :key="item.projectId" :label="item.projectName" :value="item.projectId"> </el-option>
			<template #tag>
				<span>{{ project }}</span>
			</template>
		</el-select>
	</div>
</template>

<script setup lang="ts">
import { getStore, setStore } from '@/utils/store'
import { useCommonStore } from '@/stores/common'
import apiMod from '@/apiMod/index'

const route = useRoute()
const { setGloablParams } = useCommonStore()

const pageReload: any = inject('reload')
const props = defineProps<{
	belongProject: string
}>()
const belongProject = route.meta.prefix || props.belongProject
const projectKey = `${belongProject}-project-info`
const projectInfo = getStore(projectKey)
const project = ref('')
const dataList = ref([])
const Loading = ref(false)

const belongProjectLabel = (belong) => {
	let arr = {
		govBuyManage: '政府购买',
		homeElderly: '居家上门',
		silverConsultant: '银发顾问',
		elderlyMeal: '助餐服务',
		familyNurseBed: '家庭养老床位',
	}
	return arr[belong]
}
const handleChange = (e, type?: string) => {
	const item = dataList.value.find((item) => item.projectId === e)
	const obj = {
		subprojectId: item.projectId,
		projectStatus: item.projectStatus,
		subprojectName: item.projectName,
		belongProject,
		belongProjectLabel: belongProjectLabel(belongProject),
	}
	project.value = item.projectId
	setStore('belongProject', belongProject)
	setStore(projectKey, obj)
	setGloablParams(obj)
	if (type !== 'default' || !projectInfo) {
		pageReload()
	}
}

const getInitData = () => {
	Loading.value = true
	const userInfo = getStore('userInfo') || {}
	const query = {
		belongProject,
		bindCreditCode: userInfo.organizationId,
		delFlag: 0,
		sourceType: undefined,
	}
	if (userInfo.classifyId == 3) {
	// if (userInfo.classifyId == 3 && props.belongProject == 'elderlyMeal') {
		query.sourceType = 0
	}

	apiMod
		.view_omProjectCooperate_list(query)
		.then((res) => {
			Loading.value = false
			const timeIn = (start, end) => {
				const time = new Date().getTime()
				const startTime = new Date(start).getTime()
				const endTime = new Date(end).getTime()
				return time > startTime && time < endTime
			}
			dataList.value = (res.data || []).map((item) => {
				if (item.projectStatus == 1) {
					item.projectStatus = timeIn(item.startTime, item.endTime) ? 1 : 0
				}
				return item
			})

			if (dataList.value.length == 0) {
				setStore(projectKey, '')
				project.value = ''
			} else if (!project.value) {
				handleChange(dataList.value[0]?.projectId, 'default')
			} else {
				handleChange(project.value, 'default')
			}
		})
		.catch(() => {
			Loading.value = false
		})
}

onMounted(() => {
	getInitData('init')
	if (projectInfo && route.fullPath.includes('homeCare')) {
		project.value = projectInfo.subprojectId
		setGloablParams(projectInfo)
	} else {
		setStore('belongProject', belongProject)
		setGloablParams({
			belongProject: belongProject,
			belongProjectLabel: belongProjectLabel(belongProject),
		})
	}
})

onUnmounted(() => {
	setGloablParams({})
})
</script>

<style scoped lang="scss">
:deep(.el-select) {
	--el-fill-color-blank: #0052d9;
	--el-text-color-placeholder: #ffffffde;
	--el-text-color-regular: #fff;
	--el-border-radius-base: 20px;
	--el-border-color: transparent;
}
</style>
